define(['jquery', 'knockout', 'text!./book.html', 'bootstrap', 'uui', 'director', 'grid', 'dragJW','select'],
    function ($, ko, template) {
        var viewModelBookType = {
            data: ko.observable({}),
            bookTypeDataTable: new u.DataTable({
            	id: 'booktype-grid',
                meta: {
                    'id': {},
                    'nd': {},
                    'accBookTypeCode': {},
                    'accBookTypeName': {},
                    'codeRule': {}
                }
            }),
        };

        // window.vm = viewModelBookType;

        viewModelBookType.bookTypeSelect = function () {
            var searchNd = "#booktype-search-type";
            var editNd = "#booktype-edit-nd";
            $.ajax({
                url: $ctx + "/baseData/bookType/getAllNd",
                type: "GET",
                dataType: "json",
                data: {},
                success: function (data) {
                    $(searchNd).css("color", "");
                    $(searchNd).html("");
                    $(editNd).css("color", "");
                    $(editNd).html("");
                    var please_choice = '<option>请选择会计年度</option>';
                    $(searchNd).append(please_choice);
                    for (var i = 0; i < data.length; i++) {
                        var temp = '<option>' + data[i] + '</option>';
                        $(searchNd).append(temp);
                        $(editNd).append(temp);
                    }
                }
            });
        }




        viewModelBookType.searchBookType = function () {
            var nd = $("#booktype-search-type").val();
            if(nd == "请选择会计年度"){
                nd = "";
            }
            var queryData = {};
            queryData["search_nd"] = nd;
            viewModelBookType.bookTypeDataTable.addParams(queryData);
            app.serverEvent().addDataTable("bookTypeDataTable", "all").fire({
                url: $ctx + '/evt/dispatch',
                ctrl: 'baseData.bookType.bookTypeController',
                method: 'loadBookType',
                success: function (data) {
                }
            })
        }
        
        viewModelBookType.saveBookTypeEdit = function () {
        	if (!checkInput()){
        		return;
        	}
            var id = $("#booktype-id").val();
            var nd = $("#booktype-edit-nd").val();
            var bookTypeCode = $("#booktype-code").val();
            var bookTypeName = $("#booktype-name").val();
            var bookTypeCodeRule = $("#booktype-coderule").val();
            $.ajax({
                url: $ctx + "/baseData/bookType/saveBookType",
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify({
                    id: id,
                    nd: nd,
                    accBookTypeCode: bookTypeCode,
                    accBookTypeName: bookTypeName,
                    codeRule: bookTypeCodeRule
                }),
                success: function (data) {
                    viewModelBookType.sendAjaxSuccess(data);
                }
            });
        }

        viewModelBookType.sendAjaxSuccess = function (data) {
            if (data.result == "success") {
                $("#save-success-new").css("display", "block");
                $("#save-success-new").fadeOut(3000);
                viewModelBookType.searchBookType();
                viewModelBookType.edit_notice_close();
            } else {
                if(data.saveError != undefined){
                    $("#edit-notice").modal("hide");
                    $("#error-text").text(data.saveError);
                    $("#error-notice").modal({backdrop: 'static', keyboard: false});
                } else {
                    $("#edit-notice").modal("hide");
                    $("#error-text").text(data.reason);
                    $("#error-notice").modal({backdrop: 'static', keyboard: false});
                }
            }
        }

        delBookType = function () {
            // var index = viewModelBookType.bookTypeDataTable.getCurrentRow().rowId;
            // $("#jump-delete-text").attr("class", index);
            var index = viewModelBookType.bookTypeDataTable.getCurrentIndex();
            if (index<0){
                return;
            }
            $("#delete-booktype-notice").modal({backdrop: 'static', keyboard: false});
        }

        editBookType = function () {
            var data = viewModelBookType.bookTypeDataTable.getCurrentRow().data;
            $("#booktype-id").val(data.id.value);
            $("#booktype-edit-nd").val(data.nd.value);
            $("#booktype-code").val(data.accBookTypeCode.value);
            $("#booktype-name").val(data.accBookTypeName.value);
            $("#booktype-coderule").val(data.codeRule.value);
            $("#booktype-edit-nd").attr("disabled", "disabled");
            $("#booktype-code").attr("disabled", "disabled");
            $("#edit-notice").modal({backdrop: 'static', keyboard: false});
        }


        addBookType = function () {
            $("#edit-notice").modal({backdrop: 'static', keyboard: false});
        }

        viewModelBookType.delete_booktype_close = function () {
            $("#delete-booktype-notice").modal("hide");
        }

        viewModelBookType.error_notice_close = function () {
            $("#error-notice").modal("hide");
        }

        viewModelBookType.edit_notice_close = function () {
            $("#booktype-id").val("");
            $("#booktype-edit-nd").get(0).selectedIndex=0;
            $("#booktype-code").val("");
            $("#booktype-name").val("");
            $("#booktype-coderule").val("");
            $("#booktype-edit-nd").removeAttr("disabled");
            $("#booktype-code").removeAttr("disabled");
            $("#warn-text").css("display","none");
            $("#edit-notice").modal("hide");
        }
        
        function showWarnInfo(checkResult,checkinfo){
        	if (!checkResult){
                $("#warn-text").text(checkinfo);
                if($('#warn-text').css("display")=="none")
                {
                	$("#warn-text").css("display","inline-block");
                } 
            } else {
            	$("#warn-text").css("display","none");
            }
        }
        
        /*
        viewModelBookType.checkNd = function () {
            var nd = $("#booktype-nd").val();
            //注意正则表达式中的\为转义字符，需要用\\来处理 
            var patt=new RegExp("^\\d{4}$");
            var checkResult= patt.test(nd);
            showWarnInfo(checkResult,"请输入正确年度");
            return checkResult;
        }
        */
        
        viewModelBookType.checkCode = function () {
            var code = $("#booktype-code").val();
            var patt = new RegExp("^\\d{1,9}$");
            var checkResult= patt.test(code);
            showWarnInfo(checkResult,"请输入正确科目代码：只能为数字");
            return checkResult;
        }
        
        checkInput = function () {
        	return viewModelBookType.checkCode();
        }
        
        viewModelBookType.delete_booktype = function () {
            var data = viewModelBookType.bookTypeDataTable.getCurrentRow().data;
            if ($.isEmptyObject(data)){
                return;
            }
            var id = data.id.value;
            var nd = data.nd.value;
            var bookTypeCode = data.accBookTypeCode.value;
            var jsonData = JSON.stringify({
            	id: id,
                nd: nd,
                accBookTypeCode: bookTypeCode
            });
            jsonData = "["+jsonData+"]"
            $.ajax({
                url: $ctx + "/baseData/bookType/deleteBookType",
                type: "POST",
                dataType: "json",
                data:{
                	books: jsonData
                },
                success: function (data) {
                    if (data.result == "success") {
                        $("#delete-booktype-notice").modal("hide");
                        viewModelBookType.searchBookType();
                        //3秒消失
                        $("#save-success-new").css("display", "block");
                        $("#save-success-new").fadeOut(3000);
                    } else {

                        if(data.saveError != undefined){
                            $("#delete-booktype-notice").modal("hide");
                            $("#error-text").text(data.saveError);
                            $("#error-notice").modal({backdrop: 'static', keyboard: false});
                        } else {
                            $("#delete-booktype-notice").modal("hide");
                            $("#error-text").text(data.reason);
                            $("#error-notice").modal({backdrop: 'static', keyboard: false});
                        }
                    }
                }
            });
        }

        var init = function () {
            ko.cleanNode($('.content')[0]);
            app = u.createApp(
                {
                    el: '.content',
                    model: viewModelBookType
                }
            );
            viewModelBookType.bookTypeSelect();
            viewModelBookType.searchBookType();
        };

        return {
            'model': viewModelBookType,
            'template': template,
            'init': init
        };
    }
);
